import encodings

proc create_txtx(filename: string,data:string,encoding:string) = 
    var head:array[100,char]
    for key,value in encoding:
        head[key] = value
    #echo head
    var encodeStr = encodings.convert(data,encoding,"UTF-8")
    var file = open(filename,fmWrite)
    for value in head:
        file.write(value)
    file.write(encodeStr)
    file.close()

proc get_encode(filename:string):string =
    var file = open(filename,fmRead)
    var head: array[100,char]
    var bytesRead = file.readChars(head)
    file.close()
    var encode = ""
    for value in head:
        encode.add(value)
    return encode
# 测试
proc test() = 
    create_txtx("test/chinese.txtx","中文测试","GBK")
    create_txtx("test/japanese.txtx","ああああ","shift-jis")
    echo get_encode("test/chinese.txtx")
    echo get_encode("test/japanese.txtx")
test()